import 'package:flutter/material.dart';
import 'package:kasax/commons/basic_listpage.dart';
import 'package:kasax/commons/common_entrys.dart';
import 'package:kasax/commons/commons.dart';
import 'package:kasax_demos2/entry/factory_beans.dart';
import 'package:kasax_demos2/providers/page_sturct.dart';

/// 纯列表和纯页面提供者
class PageProvider {
  static final PageProvider _instance = PageProvider._internal();

  factory PageProvider() => _instance;

  PageProvider._internal();

  static PageProvider getInstance() => _instance;

  /// 构建 界面
  Widget providePage(PageWrapper wrapper) {
    Widget? pageWidget;
    if (wrapper.pageType == PageType.LIST) {
      List<PageInfo> dataInfos = <PageInfo>[];
      switch (wrapper.supportedType) {
        case SupportListPageType.MAIN:
          dataInfos = BeansFactory.getInstance().buildMainList();
          break;
        default:
          pageWidget = buildBasicUIPage(wrapper.pageWidget!, wrapper.title);
          break;
      }
      if (pageWidget == null && dataInfos.isNotEmpty) {
        // 这里需要Scaffold包裹，否则会报错
        pageWidget = buildBasicUIPage(BasicLoadingListWidget(dataInfos), wrapper.title);
      }
    } else {
      pageWidget = buildBasicUIPage(wrapper.pageWidget!, wrapper.title);
    }
    return pageWidget!;
  }
}
